'use strict';

angular.module('app').controller('maintenanceController', [ '$rootScope', '$scope', '$http', '$state','toaster','$modal',
    function($rootScope, $scope, $http, $state,toaster,$modal) {
        $scope.title = '维修组人员信息';
        $scope.param = {};
        $scope.param.groupCode = $scope.groupCode = $state.params.groupCode;//$scope.groupCode页面上用到
        $scope.search = function () {
        	console.log($scope.param);
            $scope.loading = true;
            if($scope.pageFlag != 1){
                $scope.param.pageNum=1;
            }else{
                $scope.pageFlag = 2;
            }
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : '/dac/userManager/personMaintain/list',
                data: angular.toJson($scope.param)
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    console.log(result)
                    $scope.pageInfo = result;
                } else {
                    toaster.clear('*');
                    toaster.pop('error','', result.msg)
                }
                $scope.$apply();
            });
        }
        
        $scope.search();

        //从本地获取localstorage数据，对权限按钮做隐藏
        var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
        personAuth = personAuth.split(',');
        for(var i = 0;i<personAuth.length;i++){
            if(personAuth[i]=='main.personMaintain.read'){
                $scope.read = true;
            }
            if(personAuth[i]=='main.personMaintain.add'){
                $scope.add = true;
            }
            if(personAuth[i]=='main.personMaintain.update'){
                $scope.update = true;
            }
            if(personAuth[i]=='main.personMaintain.delete'){
                $scope.dele = true;
            }
        }

        $scope.delItem = function(id,personName) {
            if (confirm("确定删除【"+personName+"】吗？")==true)
            {
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : 'dac/userManager/personMaintain/delete',
                    data: angular.toJson({'id': id,'groupCode':$state.params.groupCode})
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        toaster.clear('*');
                        toaster.pop('success', '', "删除成功");
                        $scope.search();
                    }
                    else
                    {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                    }
                    $scope.$apply();
                });
            }
            else
            {
                toaster.clear('*');
                toaster.pop('false', '', '已取消操作');
            };

        };

        // 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };

    } ]);
